package com.daxidi.cn.controller;

import com.daxidi.cn.entity.Pay;
import com.daxidi.cn.params.QueryPayListParam;
import com.daxidi.cn.service.IPayService;
import com.daxidi.cn.utils.PageUtils;
import com.daxidi.cn.utils.ReturnResult;
import com.daxidi.cn.utils.ReturnResultUtils;
import com.daxidi.cn.vo.PaySystemListVo;
import com.daxidi.cn.vo.ShopNameVo;
import com.daxidi.cn.vo.SystemOrderDetailsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付订单表或者退款表 前端控制器
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Log4j
@Api(tags = "支付订单模块")
@RestController
@RequestMapping("/api/v1/union/pay")
public class PayController {
    @Autowired
    private IPayService payService;

    @ApiOperation(value = "支付订单模块  查询所有商家名称接口")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "查询所有商家名称成功"),
            @ApiResponse(code = 265, message = "查询所有商家名称失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @GetMapping(value = "/allShopName")
    public ReturnResult queryAllShopName(){
        try {
            List<ShopNameVo> shopNameVos = payService.queryAllShop();
            return ReturnResultUtils.returnSuccess(shopNameVos);
        } catch (Exception e) {
            log.error("查询所有商家名称失败" + e);
            return ReturnResultUtils.returnFailMsg(265, "查询所有商家名称失败");
        }
    }

    @ApiOperation(value = "支付订单模块  添加接口")
    @ApiResponses({
            @ApiResponse(code = 164, message = "添加成功"),
            @ApiResponse(code = 165, message = "添加失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PostMapping(value = "/")
    public ReturnResult addPay(@RequestBody Pay pay){
        try {
            payService.addPay(pay);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("添加失败" + e);
            return ReturnResultUtils.returnFailMsg(165, "添加失败");
        }
    }

    @ApiOperation(value = "支付订单模块  删除接口")
    @ApiResponses({
            @ApiResponse(code = 166, message = "删除成功"),
            @ApiResponse(code = 167, message = "删除失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @DeleteMapping("/{id}")
    public ReturnResult deletePayById(@PathVariable("id") Integer id){
        try {
            payService.deletePayById(id);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("删除失败" + e);
            return ReturnResultUtils.returnFailMsg(167, "删除失败");
        }
    }

    @ApiOperation(value = "支付订单模块  修改接口")
    @ApiResponses({
            @ApiResponse(code = 168, message = "修改成功"),
            @ApiResponse(code = 169, message = "修改失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PutMapping("/{id}}")
    public ReturnResult updatePayById(@RequestBody Pay pay){
        try {
            payService.updatePayById(pay);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("修改失败" + e);
            return ReturnResultUtils.returnFailMsg(169, "修改失败");
        }
    }

    @ApiOperation(value = "支付订单模块  分页查询接口",notes = "count:  为 数据的总条数")
    @ApiResponses({
            @ApiResponse(code = 170, message = "查询成功"),
            @ApiResponse(code = 171, message = "查询失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @GetMapping("/PaysPage")
    public ReturnResult<PaySystemListVo> selectPaysPage(@RequestParam(required = false) Long orderId,
                                                        @RequestParam(required = false) String payTime,
                                                        @RequestParam(required = false) Long shopId,
                                                        @RequestParam(required = false) Integer state,
                                                        @RequestParam(defaultValue = "1", required = false) Integer page,
                                                        @RequestParam(defaultValue = "20", required = false) Integer limit){
        QueryPayListParam queryPayListParam = new QueryPayListParam();
        queryPayListParam.setPayTime(payTime);
        queryPayListParam.setOrderId(orderId);
        queryPayListParam.setShopId(shopId);
        queryPayListParam.setState(state);
        try {
            PageUtils<PaySystemListVo> paySystemListVoList = payService.queryPayListPage(queryPayListParam, page, limit);
            return ReturnResultUtils.returnSuccess(paySystemListVoList);
        } catch (Exception e) {
            log.error("查询失败" + e);
            e.printStackTrace();
            return ReturnResultUtils.returnFailMsg(171, "查询失败");
        }
    }

    @ApiOperation(value = "支付订单模块  订单详情查询接口")
    @ApiResponses({
            @ApiResponse(code = 170, message = "查询成功"),
            @ApiResponse(code = 171, message = "查询失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @GetMapping("")
    public ReturnResult<PaySystemListVo> queryOrderDetails(@RequestParam String orderId){
        try {
            SystemOrderDetailsVo systemOrderDetailsVo = payService.queryOrderDetails(orderId);
            return ReturnResultUtils.returnSuccess(systemOrderDetailsVo);
        } catch (Exception e) {
            log.error("查询失败" + e);
            return ReturnResultUtils.returnFailMsg(171, "查询失败");
        }
    }



}
